package net.starwifi.duobao.web.dMember;

import net.starwifi.duobao.entity.DBShare;
import net.starwifi.duobao.entity.GiftsProduct;
import net.starwifi.duobao.entity.GiftsProductLog;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.service.GiftsProductLogService;
import net.starwifi.duobao.service.GiftsProductService;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.service.TradeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 我的礼包
 * Created by Administrator on 2016/1/27.
 */
@Controller("wxGiftsController")
@RequestMapping(value = "/dMember")
public class DMemberGiftsController extends BaseController{

    @Autowired
    GiftsProductService giftsProductService;

    @Autowired
    GiftsProductLogService giftsProductLogService;

    @Autowired
    MemberService memberService;

    @Autowired
    TradeRecordService tradeRecordService;



    /**
     * 我的礼包
     */
    @RequestMapping(value = "/gifts-list", method = RequestMethod.GET)
    public  String receiverList(HttpServletRequest request,Model model) {
        String userAgent=request.getHeader("User-Agent");
        if(userAgent.contains("MicroMessenger")){
            model.addAttribute("weixin",1);
        }
        Member member=memberService.getCurrent();
        List<Order> orderBy=new ArrayList<Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        List<GiftsProductLog> giftsProductLogs=giftsProductLogService.getListData(0,10,"o.memberId=?1", new Object[]{ member.getId()}, orderBy);
        model.addAttribute("giftsProductLogs",giftsProductLogs);
        model.addAttribute("member",member);
        return "dMember/gifts-list";
    }

    @RequestMapping(value = "/gifts-content", method = RequestMethod.GET)
    public String GiftsProductLogContent(Model model, int pageNum) {
        Member member = memberService.getCurrent();
        PageView pageView = new PageView(pageNum, 10);
        pageView.addOrder(net.starwifi.duobao.mvc.Order.desc("id"));
        pageView.addFilter(Filter.eq("memberId", member.getId()));
        Page<GiftsProductLog> page = giftsProductLogService.getScrollData(pageView);
        model.addAttribute("giftsProductLogs",page.getContent());
        return "dMember/gifts-content";
    }


    /**
     * 我的礼包
     */
    @RequestMapping(value = "/gifts-list-wx", method = RequestMethod.GET)
    public  String giftsWX(HttpServletRequest request,Model model) {
        return "redirect:/dMember/gifts-list";
    }


    /**
     * 我的礼包
     */
    @ResponseBody
    @RequestMapping(value = "/gifts-act-{id}", method = RequestMethod.POST)
    public Message receiverList(HttpServletRequest request,@PathVariable Long id,Model model) {
        Member member=memberService.getCurrent();
        GiftsProductLog giftsProductLog=giftsProductLogService.find(id);
        if(giftsProductLog==null){
            return Message.error("礼包不存在");
        }
        if(!giftsProductLog.getMemberId().equals(member.getId())){
            return Message.error("激活非法");
        }
        if(giftsProductLog.getIsActivation()){
            return Message.error("该礼包已经是激活状态");
        }
        giftsProductLog.setIsActivation(Boolean.TRUE);
        giftsProductLogService.update(giftsProductLog);
        GiftsProduct giftsProduct=giftsProductService.find(giftsProductLog.getGiftsId());
        if(giftsProduct.getType().equals("hb")){
            tradeRecordService.hb(member.getId(), BigDecimal.ONE,"（红包）夺宝优惠券",giftsProductLog.getId().toString());
        }
        return Message.success("激活成功");
    }


}
